<template>
  <div>
    <el-upload
      :action="action"
      :headers="headers"
      :multiple="false"
      :limit="1"
      :before-upload="beforeUpload"
      :on-success="onSuccess"
      :on-error="onError"
      :on-exceed="onExceed"
      :on-remove="onRemove"
      :file-list="fileList"
      :disabled="disabled"
      drag
      show-file-list
      accept="application/epub+zip"
      class="image-upload"
    >
      <i class="el-icon-upload" />
      <div
        v-if="fileList.length === 0"
        class="el-upload__text"
      >
        请将电子书拖入或<em>点击上传</em>
      </div>

      <div
        v-else
        class="el-upload__text"
      >
        <em>图书已上传</em>
      </div>
      Please Upload Ebook
    </el-upload>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
export default {
  props: {
    fileList: {
      type: Array,
      default() {
        return []
      }
    },
    disabled: {
      type: Boolean,
      default() {
        return false
      }
    }
  },
  data() {
    return {
      action: `${process.env.VUE_APP_BASE_API}/book/upload`
    }
  },
  computed: {
    headers() {
      return {
        Authorization: `Bearer ${getToken()}`
      }
    }
  },
  methods: {
    beforeUpload(file) {
      console.log(file)
      this.$emit('beforeUpload', file)
    },
    onSuccess() {
      console.log('onSuccess')
    },
    onError(err) {
      console.log({ err })
      const errMsg = (err.message && JSON.parse(err.message)) || '上传错误'
      this.$message({
        message:
          (errMsg.msg && `上传失败,失败原因: ${errMsg.msg}`) || '上传失败',
        type: 'error'
      })
    },
    onExceed() {
      console.log('onExceed')
      this.$$message({
        message: '每次只能上传一个文件',
        type: 'warning'
      })
    },
    onRemove() {
      console.log('onRemove')
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
